package com.sskj.fabi.ui.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.sskj.common.adapter.MyFragmentPagerAdapter;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.fabi.R;
import com.sskj.fabi.R2;
import com.sskj.fabi.component.DaggerUserDataComponent;
import com.sskj.fabi.presenter.FabiFragmentPresenter;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseFragment;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;

import java.util.ArrayList;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

@Route(path = RConfig.FABI_FRAGMENT)
public class FabiFragment extends BaseFragment<FabiFragmentPresenter> {

    @BindView(R2.id.viewPager)
    ViewPager viewPager;
    @BindView(R2.id.tvSell)
    TextView tvSell;
    @BindView(R2.id.viewSell)
    View viewSell;
    @BindView(R2.id.llSell)
    RelativeLayout llSell;
    @BindView(R2.id.tvBuy)
    TextView tvBuy;
    @BindView(R2.id.viewBuy)
    View viewBuy;
    @BindView(R2.id.llBuy)
    RelativeLayout llBuy;
    @BindView(R2.id.tvOrder)
    TextView tvOrder;
    @BindView(R2.id.viewPublish)
    View viewPublish;
    private boolean isBuy = false;
    @Inject
    UserViewModel userViewModel;
    private UserData userData;


    @Override
    protected int getLayoutId() {
        return R.layout.fabi_fragment;
    }

    @Override
    public FabiFragmentPresenter getPresenter() {
        return new FabiFragmentPresenter();
    }

    @Override
    public void initView() {

        DaggerUserDataComponent.create().inject(this);
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
            } else {
                userData = null;
            }
            updateUI();
        });
        ArrayList<Fragment> fragments = new ArrayList<>();
        fragments.add((Fragment) ARouter.getInstance().build(RConfig.FABI_FRAGMENT_MARKET_BUY_AND_SELL).withBoolean(Constans.IS_BUY, false).navigation());
        fragments.add((Fragment) ARouter.getInstance().build(RConfig.FABI_FRAGMENT_MARKET_BUY_AND_SELL).withBoolean(Constans.IS_BUY, true).navigation());

        viewPager.setAdapter(new MyFragmentPagerAdapter(getChildFragmentManager(), fragments));
        ClickUtil.click(llBuy, () -> {

            if (!isBuy) {
                isBuy = true;
                changeTab();
            }
        });
        ClickUtil.click(llSell, () -> {

            if (isBuy) {
                isBuy = false;
                changeTab();
            }
        });
        ClickUtil.click(tvOrder, () -> {
            ARouter.getInstance().build(RConfig.FABI_ORDER_RECORD).navigation();
        });
        ClickUtil.click(viewPublish, () -> {
            ARouter.getInstance().build(RConfig.FABI_PUBLISH).withBoolean(Constans.IS_BUY, isBuy).navigation();
        });
    }

    private void updateUI() {
        if (userData == null) {
            viewPublish.setVisibility(View.GONE);
            tvOrder.setVisibility(View.GONE);
        } else {
            viewPublish.setVisibility(View.VISIBLE);
            tvOrder.setVisibility(View.VISIBLE);
        }
    }

    private void changeTab() {
        viewBuy.setVisibility(isBuy ? View.VISIBLE : View.INVISIBLE);
        viewSell.setVisibility(!isBuy ? View.VISIBLE : View.INVISIBLE);
        tvBuy.setTextColor(ContextCompat.getColor(App.INSTANCE, isBuy ? R.color.fabi_f53 : R.color.fabi_6b7));
        tvSell.setTextColor(ContextCompat.getColor(App.INSTANCE, !isBuy ? R.color.fabi_05c : R.color.fabi_6b7));
        viewPager.setCurrentItem(isBuy ? 1 : 0);
        viewPublish.setBackground(ContextCompat.getDrawable(App.INSTANCE, isBuy ? R.mipmap.fabi_icon_publish_f53 : R.mipmap.fabi_icon_publish_05c));

    }


}
